
package com.yxjx.duoxue.payment;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.yxjx.duoxue.BaseActionBarActivity;
import com.yxjx.duoxue.R;
import com.yxjx.duoxue.Yx;
import com.yxjx.duoxue.customview.YxListView;
import com.yxjx.duoxue.datamodel.DMCoupon;
import com.yxjx.duoxue.datamodel.DMObject;
import com.yxjx.duoxue.datamodel.DMReportErrorItem;
import com.yxjx.duoxue.network.UserReg;
import com.yxjx.duoxue.utils.UIHelper;
import com.yxjx.duoxue.utils.Utils;

public class CouponListActivity extends BaseActionBarActivity {

	private static final int MSG_LOAD_COUPON_LIST_DONE = 1;
	private MyAdapter mAdapter;
	private ListView list;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_LOAD_COUPON_LIST_DONE:
				ArrayList<DMCoupon> items = null;
				if (msg.obj != null) {
					items = (ArrayList<DMCoupon>) msg.obj;
				}
				mAdapter.setData(items);
				break;
			default:
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_usable_coupons);
		customizeActionBarTitle("我的奖品/代金券");

		showLoading();
		list = (ListView) findViewById(R.id.list);
		mAdapter = new MyAdapter();
		list.setAdapter(mAdapter);

		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				ArrayList<DMCoupon> list = mAdapter.getData();
				boolean state = list.get(arg2).isSelected();
				list.get(arg2).setSelected(!state);
				for (int i = 0; i < list.size(); i++) {
					if (list.get(arg2).isSelected() && i != arg2) {
						list.get(i).setSelected(false);
					}
				}
				mAdapter.notifyDataSetInvalidated();
			}
		});

		UIHelper.setClickListener(findViewById(R.id.root), R.id.submit,
				new View.OnClickListener() {
					@Override
					public void onClick(View arg0) {
						DMCoupon item = mAdapter.getSelectedCoupon();
						if (item != null) {
							Intent intent = new Intent();
							intent.putExtra(Yx.EXTRA_COUPON_OBJECT, item);
							setResult(RESULT_OK, intent);
							finish();
						} else {
							UIHelper.showTips(list, "请选择一个代金券");
						}
					}
				});
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				ArrayList<DMCoupon> items = null;
				try {
					items = (ArrayList<DMCoupon>) getIntent().getSerializableExtra(Yx.EXTRA_COUPON_LIST);;
					// 提交订单与优惠券列表来回切换时，传递优惠券选中状态。
					if (items != null) {
						Object couponItem = getIntent().getExtras().get(Yx.EXTRA_COUPON_OBJECT);
						if (couponItem != null) {
							for (DMCoupon item : items) {
								if (((DMCoupon) couponItem).getCouponId() == item.getCouponId()) {
									item.setSelected(true);
									break;
								}
							}
						}
					}
				} catch (ParseException e) {
					e.printStackTrace();
				} finally {
					handler.sendMessage(handler.obtainMessage(MSG_LOAD_COUPON_LIST_DONE, items));
				}
				
			}
		}).start();
	}

	private void showLoading() {
		findViewById(R.id.root).setVisibility(View.INVISIBLE);
		findViewById(R.id.loading).setVisibility(View.VISIBLE);
		findViewById(R.id.empty).setVisibility(View.INVISIBLE);
	}

	private void showEmpty() {
		findViewById(R.id.root).setVisibility(View.INVISIBLE);
		findViewById(R.id.loading).setVisibility(View.INVISIBLE);
		findViewById(R.id.empty).setVisibility(View.VISIBLE);
	}

	private void showNormal() {
		findViewById(R.id.root).setVisibility(View.VISIBLE);
		findViewById(R.id.loading).setVisibility(View.INVISIBLE);
		findViewById(R.id.empty).setVisibility(View.INVISIBLE);
	}

	class MyAdapter extends BaseAdapter {

		private ArrayList<DMCoupon> mData;

		public DMCoupon getSelectedCoupon() {
			if (mData != null && mData.size() > 0) {
				for (DMCoupon item : mData) {
					if (item.isSelected()) {
						return item;
					}
				}
			}
			return null;
		}

		@Override
		public int getCount() {
			return mData == null ? 0 : mData.size();
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View ret = null;
			if (arg1 == null) {
				ret = LayoutInflater.from(CouponListActivity.this).inflate(
						R.layout.item_coupon_list, null);
			} else {
				ret = arg1;
			}
			DMCoupon item = mData.get(arg0);
			UIHelper.setText((TextView) ret.findViewById(R.id.coupon_fee), "" + item.getCouponPrice());
			UIHelper.setText((TextView) ret.findViewById(R.id.coupon_title), item.getCouponName());
			UIHelper.setText((TextView) ret.findViewById(R.id.coupon_des), "" + Utils.getTimeDuration(item.getStartTime(), item.getEndTime()));
			ret.findViewById(R.id.icon).setSelected(item.isSelected());
			return ret;
		}

		public ArrayList<DMCoupon> getData() {
			return mData;
		}

		public void setData(ArrayList<DMCoupon> mData) {
			this.mData = mData;
			notifyDataSetChanged();
			if (mData == null || mData.size() == 0) {
				showEmpty();
			} else {
				showNormal();
			}
		}
	}

}
